//
//  WXAliTradeServiceModule.swift
//  laxiaoke
//
//  Created by 吕凌 on 2018/4/10.
//  Copyright © 2018年 吕凌. All rights reserved.
//

import Foundation

public extension WXAliTradeServiceModule {
    
    func navigator() -> WXNavigationProtocol? {
        let navigator = WXHandlerFactory.handler(for: WXNavigationProtocol.self)
        return navigator as! WXNavigationProtocol
    }
    
    @objc func couponPage(_ params: NSDictionary, callback: @escaping WXModuleKeepAliveCallback) {
        let url = params["url"]! as! String
        _ = AlibcTradePageFactory.page(url)
        let showParam = AlibcTradeShowParams()
        if(params["openType"] != nil && params["openType"] as! String == "native") {
            showParam.openType = AlibcOpenType.native
        } else {
            showParam.openType = AlibcOpenType.auto
        }
//        let webview: UIWebView = UIWebView.init()
        let res = AlibcTradeSDK.sharedInstance().tradeService()?.open(byUrl: url, identity: "trade", webView: nil, parentController: weexInstance.viewController, showParams: showParam, taoKeParams: nil, trackParam: nil,
            tradeProcessSuccessCallback: {(result) in
                callback(result, false)
            },
            tradeProcessFailedCallback: {(error) in
                
        })
        if (res == 1) {
//            self.navigator()?.pushViewController(withParam: nil, completion: { (str, hashable) in
//
//            }, withContainer: weexInstance.viewController)
        }
    }
    
    @objc func openByUrl(_ params: NSDictionary, callback: @escaping WXModuleKeepAliveCallback) {
        self.couponPage(params, callback: callback)
    }
    
    @objc func openByBizCode(_ params: NSDictionary, callback: @escaping WXModuleKeepAliveCallback) {
            let url = params["url"]! as! String
            let bizCode = params["bizCode"]! as! String
            _ = AlibcTradePageFactory.page(url)
            let showParam = AlibcTradeShowParams()
            if(params["openType"] != nil && params["openType"] as! String == "native") {
                showParam.openType = AlibcOpenType.native
            } else {
                showParam.openType = AlibcOpenType.auto
            }
    //        let webview: UIWebView = UIWebView.init()
        let page = AlibcTradePageFactory.page(url)
        AlibcTradeSDK.sharedInstance()?.tradeService()?.open(byBizCode: bizCode, page: page, webView: nil, parentController: weexInstance.viewController, showParams:showParam, taoKeParams: nil, trackParam: nil, tradeProcessSuccessCallback: {(result) in
            callback(result, false)
        }, tradeProcessFailedCallback: {(error) in})
//            AlibcTradeSDK.sharedInstance().tradeService()?.open(byBizCode: url, identity: "trade", webView: nil, parentController: weexInstance.viewController, showParams: showParam, taoKeParams: nil, trackParam: nil,
//                tradeProcessSuccessCallback: {(result) in
//                    callback(result, false)
//                },
//                tradeProcessFailedCallback: {(error) in})
        }
    
    @objc func open(_ params: NSDictionary) {
        _ = AlibcTradeTaokeParams()
        _ = AlibcTradePageFactory.page(params["url"]! as! String)
        let url = params["url"]! as! String
        let showParam = AlibcTradeShowParams()
        if(params["openType"] == nil || params["openType"] as! String == "native") {
            showParam.openType = AlibcOpenType.native
        } else {
            showParam.openType = AlibcOpenType.auto
        }
        AlibcTradeSDK.sharedInstance().tradeService()?.open(byUrl: url, identity: "trade", webView: nil, parentController: weexInstance.viewController, showParams: showParam, taoKeParams: nil, trackParam: nil, tradeProcessSuccessCallback: {(result) in}, tradeProcessFailedCallback: {(error) in})
    }
    
    @objc func taobaoAuth(_ callback: @escaping WXModuleKeepAliveCallback) {
        ALBBSDK.sharedInstance()?.logout(callback: {
            self.doTaobaoAuth(callback)
        })
    }
    
    @objc func doTaobaoAuth(_ callback: @escaping WXModuleKeepAliveCallback) {
        var result:Dictionary = Dictionary<String,Any>()
        ALBBSDK.sharedInstance()?.setAuthOption(NormalAuth)
        ALBBSDK.sharedInstance()?.auth(weexInstance.viewController, successCallback: { (session) in
            result["ok"] = true
            let user:ALBBUser = (session?.getUser())!
            var _user:Dictionary = Dictionary<String,Any>()
            _user["avatarUrl"] = user.avatarUrl
            _user["nick"] = user.nick
            _user["openId"] = user.openId
            _user["openSid"] = user.openSid
            _user["topAccessToken"] = user.topAccessToken
            _user["topAuthCode"] = user.topAuthCode
            result["user"] = _user
            callback(result, false)
        }, failureCallback: { (session, error) in
            result["ok"] = false
            result["error"] = error
            callback(result, false)
        })
    }
}
